//
//  WGQConsultationImagesTableViewCell.m
//  MaYi
//
//  Created by 小破夕 on 2018/9/5.
//  Copyright © 2018年 JUMI. All rights reserved.
//  首页资讯的多图cell

#import "WGQConsultationImagesTableViewCell.h"
#import "WGQConsultationImagesCollectionCell.h"

@interface WGQConsultationImagesTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UIView * backView;
@property (nonatomic,strong) UIImageView * iconImageView;//左上角的头像图片
@property (nonatomic,strong) UILabel * nameLabel;//名称label
@property (nonatomic,strong) UILabel * senderLabel;//发送人label
@property (nonatomic,strong) UIButton * closeButton;//右上角的关闭button
@property (nonatomic,strong) UILabel * contentLabel;//内容label
@property (nonatomic,strong) UILabel * titleLabel;//标题label
@property (nonatomic,strong) UICollectionView * collectionView;//展示多张图片的collection
@property (nonatomic,strong) UILabel * timeLabel;//时间label
@property (nonatomic,strong) UILabel * signLabel;//右下角的标志label
@property (nonatomic,strong) UIImageView * signImageView;//右下角的标志图片

@property (nonatomic,strong) NSMutableArray * collectionArray;

@end

@implementation WGQConsultationImagesTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.collectionArray = [NSMutableArray array];
        self.contentView.backgroundColor = FFColor(0xebebeb);
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews
{
    //背景色，并且有圆角
    self.backView = [[UIView alloc] init];
    self.backView.backgroundColor = [UIColor whiteColor];
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.cornerRadius = 7 * kScale;
    [self.contentView addSubview:self.backView];
    
    //左上角的头像图片
    self.iconImageView = [[UIImageView alloc] init];
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.layer.cornerRadius = 20 * kScale;
    [self.contentView addSubview:self.iconImageView];
    
    //名称label
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont fontWithName:kMainFontFormat size:18 * kScale];
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    self.nameLabel.textColor = FFColor(0x333333);;
    [self.contentView addSubview:self.nameLabel];
    
    //发送人label
    self.senderLabel = [[UILabel alloc] init];
    self.senderLabel.font = [UIFont fontWithName:kMainFontFormat size:14 * kScale];
    self.senderLabel.textAlignment = NSTextAlignmentLeft;
    self.senderLabel.textColor = FFColor(0x333333);;
    [self.contentView addSubview:self.senderLabel];
    
    //右上角的关闭button
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeButton setImage:[UIImage imageNamed:@"closebutton_logo"] forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(closeButtonActionWithSender:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.closeButton];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont fontWithName:kMainFontFormat size:14 * kScale];
    self.titleLabel.textColor = FFColor(0x333333);
    [self.contentView addSubview:self.titleLabel];
    
    //内容label
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.contentLabel.textAlignment = NSTextAlignmentLeft;
    self.contentLabel.font = [UIFont fontWithName:kMainFontFormat size:14 * kScale];
    self.contentLabel.textColor = FFColor(0x333333);;
    [self.contentView addSubview:self.contentLabel];
    
    //盛放item的collection
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 2 * kScale;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //计算items的frame的时候,使用系统的导航栏,导航栏的高度
    layout.itemSize = CGSizeMake(108 * kScale,68 * kScale);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    if (@available(iOS 11.0, *))
    {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[WGQConsultationImagesCollectionCell class] forCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([WGQConsultationImagesCollectionCell class])]];
    [self.contentView addSubview:self.collectionView];
    
    //时间label
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = [UIFont fontWithName:kMainFontFormat size:12 * kScale];
    self.timeLabel.textAlignment = NSTextAlignmentLeft;
    self.timeLabel.textColor = FFColor(0x999999);
    [self.contentView addSubview:self.timeLabel];
    
    //右下角的标志label
    self.signLabel = [[UILabel alloc] init];
    self.signLabel.font = [UIFont fontWithName:kMainFontFormat size:10 * kScale];
    self.signLabel.textAlignment = NSTextAlignmentCenter;
    self.signLabel.textColor = FFColor(0x999999);
    [self.contentView addSubview:self.signLabel];
    
    //右下角的标志图片
    self.signImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.signImageView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.contentView.mas_left).offset(4 * kScale);
         make.right.equalTo(self.contentView.mas_right).offset(-4 * kScale);
         make.top.equalTo(self.contentView.mas_top);
         make.bottom.equalTo(self.contentView.mas_bottom);
     }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.width.and.height.mas_equalTo(40 * kScale);
         make.top.equalTo(self.contentView.mas_top).offset(12 * kScale);
         make.left.equalTo(self.contentView).offset(16 * kScale);
     }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.iconImageView.mas_right).offset(12 * kScale);
         make.top.equalTo(self.iconImageView.mas_top);
     }];
    
    [self.senderLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.nameLabel);
         make.top.equalTo(self.nameLabel.mas_bottom);
     }];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.contentView.mas_top).offset(12 * kScale);
         make.right.equalTo(self.contentView.mas_right).offset(-16 * kScale);
         make.width.and.height.mas_equalTo(16 * kScale);
     }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.iconImageView.mas_bottom).offset(12 * kScale);
         make.left.equalTo(self.iconImageView.mas_left);
         make.right.equalTo(self.contentView.mas_right).offset(-16 * kScale);
     }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.titleLabel.mas_left);
         make.top.equalTo(self.titleLabel.mas_bottom);
         make.right.equalTo(self.titleLabel.mas_right);
     }];
    
    if (self.collectionArray.count <= 3)
    {
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.left.equalTo(self.contentLabel);
             make.right.equalTo(self.contentLabel);
             make.top.equalTo(self.contentLabel.mas_bottom).offset(12 * kScale);
             make.height.mas_equalTo(68 * kScale);
         }];
    }
    else
    {
        if (self.collectionArray.count % 3 == 0)
        {
            [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make)
             {
                 make.left.equalTo(self.contentLabel);
                 make.right.equalTo(self.contentLabel);
                 make.top.equalTo(self.contentLabel.mas_bottom).offset(12 * kScale);
                 make.height.mas_equalTo(self.collectionArray.count/3 * 68 * kScale + (self.collectionArray.count/3- 1) * kScale * 4);
             }];
        }
        else
        {
            NSInteger count = self.collectionArray.count/3;
            [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make)
             {
                 make.left.equalTo(self.contentLabel);
                 make.right.equalTo(self.contentLabel);
                 make.top.equalTo(self.contentLabel.mas_bottom).offset(12 * kScale);
                 make.height.mas_equalTo((count + 1) * 68 * kScale + (count/3) * kScale * 4);
             }];
        }
    }
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.collectionView.mas_bottom).offset(12 * kScale);
         make.left.equalTo(self.contentView.mas_left).offset(16 * kScale);
//         make.bottom.equalTo(self.contentView.mas_bottom).offset(-12 * kScale);
     }];
    
    [self.signLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.bottom.equalTo(self.timeLabel.mas_bottom);
         make.right.equalTo(self.contentView.mas_right).offset(-16 * kScale);
     }];
    
    [self.signImageView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.centerY.equalTo(self.timeLabel.mas_centerY);
         make.right.equalTo(self.signLabel.mas_left).offset(-4 * kScale);
         make.width.and.height.mas_equalTo(16 * kScale);
     }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WGQConsultationImagesCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([WGQConsultationImagesCollectionCell class])] forIndexPath:indexPath];
    [cell setObject:self.collectionArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.image_select_click)
    {
        self.image_select_click(self.collectionArray[indexPath.row]);
    }
}

- (void)closeButtonActionWithSender:(UIButton *)sender
{
    if (self.delete_click)
    {
        self.delete_click(YES);
    }
}

- (void)setObject:(id)object
{
    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary * model = object;
        NSString *imageUrl = [NSString stringWithFormat:@"%@%@", IMG_OSS, model[@"sender_img"]];
        
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        
        self.nameLabel.text = model[@"sender_name"];
        self.senderLabel.text = [NSString stringWithFormat:@"发送人：%@", model[@"sender_username"]];
//        self.titleLabel.text = model.message_title;
        self.contentLabel.text = model[@"message_content"];;
        self.timeLabel.text = model[@"send_time"];
//        [self.signImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kImageUrl,model.functionimage]]];
//        self.signLabel.text = model.message_type;
        
        NSArray * array = [model[@"message_images"] componentsSeparatedByString:@","];//分隔符
        self.collectionArray = [NSMutableArray arrayWithArray:array];
        [self.collectionView reloadData];
        [self layoutSubviews];
    }
}

+ (CGFloat)heightWithModel:(NSDictionary *)model
{
    WGQConsultationImagesTableViewCell *cell = [[WGQConsultationImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithUTF8String:object_getClassName([WGQConsultationImagesTableViewCell class])]];
    [cell setObject:model];
    [cell layoutIfNeeded];
    CGRect frame = cell.timeLabel.frame;
    return frame.origin.y + frame.size.height + 10 * kScale;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
